#pragma once

#include "timer.h"
#include "input.h"

#include <Windows.h>
#include <string>
#include <vector>

struct Entry {
	Entry(const int pos, const std::string& value, const int attr, const int highlight_attr)
	:pos_(pos), value_(value), attr_(attr), highlight_attr_(highlight_attr) {}

	int pos_;
	std::string value_;
	int attr_;
	int highlight_attr_;
};

class ConsoleImpl {
public:
	ConsoleImpl(const std::string& title, const int width, const int height);
	~ConsoleImpl();
	int GetEntryIndex(const int pos) const;
	void SetSize(const int width, const int height);
	void SetTitle(const std::string& title);
	int WriteString(const int row, const int col, const std::string& value, const int attr, const int highlight_attr);
	float TimeElapsedSinceLastFlush() const;
	void Flush();
	void ReadInputs(std::vector<Input>& inputs);

private:
	void ClearBuffer();

	HANDLE wHnd_;    // Handle to write to the console.
	HANDLE rHnd_;    // Handle to read from the console.
	CHAR_INFO* buffer_;
	int width_;
	int height_;
	std::string title_;
	Timer timer_;
	float time_of_last_flush_;
	std::vector<Entry> entries_;
	
	COORD last_mouse_pos_;
	DWORD last_mouse_buttons_state_;

	friend class Console;
};

